package com.ruoyi.common.hyq;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 分页
 * @param <T>
 */
@ApiModel("分页信息")
public class PageEntity<T> implements Serializable {
    @JsonIgnore
    private int pageSize = 20;
    @JsonIgnore
    private int pageNum = 1;
    @ApiModelProperty("总条数")
    private int total = 0;
    @JsonIgnore
    private int pageTotal = 0;
    @ApiModelProperty("当前页数据")
    private List<T> rows;

    public PageEntity() {

    }

    public PageEntity(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageEntity(int total, int pageSize, List<T> content) {
        this.total = total;
        this.pageSize = pageSize;
        this.rows = content;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageTotal() {
        if (total % pageSize == 0) {
            return total / pageSize;
        } else {
            return (total / pageSize) + 1;
        }
    }

    public static List findPageContent(List list, PageEntity page) {
        if (page != null && !CollectionUtil.isEmpty(list)) {
            int rows = list.size();
            int start = (page.getPageNum() - 1) * page.getPageSize();
            int end = page.getPageNum() * page.getPageSize();
            if (end > rows) {
                end = rows;
            }
            //如果起始数大于总列表数，重置为第一页
            if (start > rows || start < 0) {
                if (rows > page.getPageSize()) {
                    return list.subList(0, page.pageSize);
                } else {
                    return list.subList(0, rows);
                }
            }
            return list.subList(start, end);
        }
        return null;
    }
}
